<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Account;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class AccountController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid(): Grid
    {
        return Grid::make(new Account(), function (Grid $grid) {
            $grid->model()->with(['user'])->orderByDesc('id');
            $grid->column('id');
            $grid->column('user.tiktokno', trans('user.fields.tiktokno'))->display(function ($var){
                return $this->user_id."({$var})";
            });
            $grid->column('amount')->display(function ($var) {
                return $var / 100;
            });
            $grid->column('type')->display(function ($var) {
                $arr = trans('validation.account_type');
                return $arr[$var];
            });
            //$grid->column('childUser.phone', trans('account.fields.childUser'));
            $grid->column('order_id');
            $grid->column('created_at');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('user.id', trans('user.fields.id'));
                $filter->like('user.tiktokno', trans('user.fields.tiktokno'));
                $filter->like('order_id');
                $filter->equal('type')->select(trans('validation.account_type'));

            });

            $grid->disableRowSelector();
            $grid->disableCreateButton();
            $grid->disableActions();
            $grid->export();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Account(), function (Show $show) {
            $show->field('id');
            $show->field('order_id');
            $show->field('type');
            $show->field('user_id');
            $show->field('child_user_id');
            $show->field('amount');
            $show->field('balance');
            $show->field('level');
            $show->field('rebate_rate');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Account(), function (Form $form) {
            $form->display('id');
            $form->text('order_id');
            $form->text('type');
            $form->text('user_id');
            $form->text('child_user_id');
            $form->text('amount');
            $form->text('balance');
            $form->text('level');
            $form->text('rebate_rate');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
